package com.jumper.study.admin.controller;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jumper.study.admin.dto.req.LessonEbookReq;
import com.jumper.study.admin.dto.req.LessonEbookBatchReq;
import com.jumper.study.admin.dto.req.LessonEbookItem;
import com.jumper.study.admin.dto.req.ModelStatusReq;
import com.jumper.study.admin.dto.resp.LessonEbookResp;
import com.jumper.study.admin.service.LessonEbookService;
import com.jumper.study.admin.service.LessonService;
import com.jumper.study.common.api.CommonResult;
import com.jumper.study.common.api.ResultCode;
import com.jumper.study.db.model.LessonEbook;
import com.jumper.study.db.model.Lesson;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;



@RestController
@RequestMapping("/api/backend/lessons/ebooks")
public class LessonEbookController {
    @Autowired
    private LessonEbookService ebookService;

    @Autowired
    private LessonService lessonService;

    @GetMapping("/{id}")
    public CommonResult<LessonEbookResp> getItem(@PathVariable Long id) {
        LessonEbook ebook = ebookService.getById(id);
        if (ebook == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), ResultCode.NOT_FOUND.getMessage());
        }
        Lesson lesson = lessonService.getById(ebook.getLessonId());
        if (lesson == null) {
            return CommonResult.failed(ResultCode.NOT_FOUND.getCode(), "课程不存在");
        }

        LessonEbookResp resp = new LessonEbookResp(ebook.getId(), ebook.getLessonId(), ebook.getTitle(), 
                lesson.getTitle(), ebook.getFile(), ebook.getOnself(), ebook.getSort(), ebook.getUpdatedAt());

        return CommonResult.success(resp);
    }

    @PostMapping("/")
    public CommonResult<Boolean> create(@RequestBody @Validated LessonEbookReq req) {
        Lesson lesson = lessonService.getById(req.getLessonId());
        if (lesson == null) {
            return CommonResult.failed(ResultCode.VALIDATE_FAILED.getCode(), "课程不存在");
        }
        LessonEbook ebook = new LessonEbook();
        ebook.setTitle(req.getTitle());
        ebook.setLessonId(req.getLessonId());
        ebook.setFile(req.getFile());
        ebook.setOnself(false);
        ebook.setSort(req.getSort());
        boolean result = ebookService.save(ebook);

        return CommonResult.success(result);
    }

    @PostMapping("/batch")
    public CommonResult<Boolean> createBatch(@RequestBody @Validated LessonEbookBatchReq req) {
        Lesson lesson = lessonService.getById(req.getLessonId());
        if (lesson == null) {
            return CommonResult.notFound("课程不存在");
        }

        List<LessonEbook> ebooks = new ArrayList<>();
        for(LessonEbookItem file : req.getFiles()) {
            LessonEbook ebook = new LessonEbook();
            ebook.setLessonId(req.getLessonId());
            ebook.setTitle(file.getTitle());
            ebook.setFile(file.getFile());
            ebook.setSort(file.getSort());
            ebook.setOnself(false);
            ebooks.add(ebook);
        }
        boolean result = ebookService.saveBatch(ebooks);

        return CommonResult.success(result);
    }
    
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        LessonEbook ebook = ebookService.getById(id);
        if (ebook == null) {
            return CommonResult.success(true);
        }

        boolean result = ebookService.removeById(id);
        return CommonResult.success(result);
    }

    @PostMapping("/onself")
    public CommonResult<Boolean> onself(@RequestBody @Validated ModelStatusReq req) {
        boolean result = ebookService.onselfBatchById(req.getIds(), req.status());

        return CommonResult.success(result);
    }
    
}
